﻿#include "personal_info_m.h"

//清理缓存区
void clear_input_buffer2() {
	int c;
	while ((c = getchar()) != '\n' && c != EOF); // 读取到换行符或文件尾
}
//创建链表节点
User_node* create_node() {
	User_node* node = malloc(sizeof(User_node));
	if (node) {
		memset(&node->user, 0, sizeof(User)); // 清零初始化
		node->next = NULL;
	}
	return node;
}
//链表初始化
void init_list(User_list* user_list) {
	user_list->head = NULL;
	user_list->size = 0;
}
//加入链表
void append_list(User_list* user_list, User_node* user_node) {
	if (user_list == NULL || user_node == NULL) {
		printf("参数错误！\n");
		return;
	}

	if (user_list->head == NULL) {
		user_list->head = user_node;
	}
	else {
		User_node* cur_node = user_list->head;
		while (cur_node->next != NULL) {
			cur_node = cur_node->next;
		}
		cur_node->next = user_node;

	}
	user_list->size++;
}
//打印当前链表
void printf_user(User_list user_list) {

	User_node* cur_node = user_list.head;
	if (cur_node)
		while (cur_node != NULL) {
			printf("%s\t%s\t%s\t%s\t%s\n", 
				cur_node->user.id_nicknames, 
				cur_node->user.phone, 
				cur_node->user.secret_code, 
				cur_node->user.name, 
				cur_node->user.id_name);
			cur_node = cur_node->next;
		}
}
//清空当前链表
void free_list(User_list* user_list) {
	User_node* cur_node = user_list->head;
	User_node* q_node = NULL;
	while (cur_node != NULL) {
		q_node = cur_node->next;
		free(cur_node);
		cur_node = q_node;
		user_list->size--;
		/*printf("已清一个\n");*/
	}
	user_list->head = NULL;
}
//按值查找某个节点
User_node* find_phone_data(User_list user_list, char phone[]) {
	User_node* cur_node = user_list.head;
	while (cur_node != NULL) {
		if (!strcmp(cur_node->user.phone, phone)) return cur_node;
		cur_node = cur_node->next;
	}
	return NULL;

}
//按电话号找用户
User* find_user_by_phone(User_list* user_list, char* phone) {
	User_node* cur_node = user_list->head;
	while (cur_node != NULL) {
		if (!strcmp(cur_node->user.phone, phone)) return &cur_node->user;
		cur_node = cur_node->next;
	}
	return NULL;
}
//注册用户信息
void resigter_st(User_list* user_list) {

	//未考虑建设性测试的代码

	system("cls");
	printf("*******************************************\n");
	printf("*            正在为您注册                 *\n");
	printf("*******************************************\n");

	//创建节点
	User_node* user_node = create_node();



	printf("*            请输入您的手机号             *\n");
	scanf_s("%s", user_node->user.phone,50);
	clear_input_buffer2();
	system("cls");
	printf("*******************************************\n");
	printf("*            正在为您注册                 *\n");
	printf("*******************************************\n");
	printf("*            设置你的密码                 *\n");
	scanf_s("%s", user_node->user.secret_code,50);
	clear_input_buffer2();
	system("cls");
	printf("*******************************************\n");
	printf("*            正在进行实名认证             *\n");
	printf("*******************************************\n");
	printf("*            请输入你的真实姓名         *\n");
	scanf_s("%s", user_node->user.name,50);
	clear_input_buffer2();
	system("cls");
	printf("*******************************************\n");
	printf("*            正在进行实名认证             *\n");
	printf("*******************************************\n");
	printf("*            请输入你的身份证号码         *\n");
	scanf_s("%s", user_node->user.id_name,50);
	clear_input_buffer2();
	system("cls");
	printf("*******************************************\n");
	printf("*            正在为您注册                 *\n");
	printf("*******************************************\n");
	printf("*            取一个好听的昵称吧           *\n");
	scanf_s("%s", user_node->user.id_nicknames,50);
	clear_input_buffer2();
	//加入信息至链表中
	append_list(user_list, user_node);
	/*user_node->next = user_list->head;
	user_list->head = user_node;
	user_list->size++;*/

}
//保存用户信息
void save_uers_data(User_list* user_list) {

	//打开文件
	FILE* fp = fopen("..\\src\\users_data.txt", "w");
	if (!fp) {
		perror("file open failed");
		return;
	}

	//写入用户信息
	User_node* cur_node = user_list->head;
	while (cur_node != NULL) {

		fprintf(fp,"%s\t%s\t%s\t%s\t%s\n",
			cur_node->user.id_nicknames,
			cur_node->user.phone,
			cur_node->user.secret_code,
			cur_node->user.name,
			cur_node->user.id_name);
		cur_node = cur_node->next;
	}

	//关闭文件
	fclose(fp);

}
//保存更改之后的用户信息
void save_change_uers_data(User_list* user_list) {

	//打开文件
	FILE* fp = fopen("..\\src\\users_data.txt", "w");
	if (!fp) {
		perror("file open failed");
		return;
	}

	//写入用户信息
	User_node* cur_node = user_list->head;
	while (cur_node != NULL) {

		fprintf(fp, "%s\t%s\t%s\t%s\t%s\n", 
			cur_node->user.id_nicknames, 
			cur_node->user.phone, 
			cur_node->user.secret_code, 
			cur_node->user.name, 
			cur_node->user.id_name);
		cur_node = cur_node->next;
	}

	//关闭文件
	fclose(fp);

}
//读取用户信息
void read_user_data(User_list* user_list) {

	//打开文件
	FILE* fp = fopen("..\\src\\users_data.txt", "r");
	if (!fp) {
		perror("file open failed");
		return;
	}


	while (true) {

		User_node* user_node = create_node();
		int read = fscanf_s(fp,"%s %s %s %s %s", 
			user_node->user.id_nicknames, 50,
			user_node->user.phone, 50,
			user_node->user.secret_code, 50,
			user_node->user.name, 50,
			user_node->user.id_name,50);
		if (read != 5) {
			free(user_node);
			break;
		}
		append_list(user_list, user_node);

	}


	//关闭文件
	fclose(fp);

}
//判断账号信息
bool judge_user_data(User_list user_list, User_node** cur_user_node) {
	char judge_phone[50];
	char judge_secret_code[50];
	//system("pause");
	system("cls");
	printf("*******************************************\n");
	printf("*            正在为您登录                 *\n");
	printf("*******************************************\n");
	printf("*            请输入您的手机号             *\n");
	scanf_s("%s", judge_phone,50);
	clear_input_buffer2();
	system("cls");
	printf("*******************************************\n");
	printf("*            正在为您登录                 *\n");
	printf("*******************************************\n");
	printf("*            请输入您的密码               *\n");
	scanf_s("%s", judge_secret_code,50);
	clear_input_buffer2();
	User_node* cur_node = user_list.head;
	while (cur_node != NULL && strcmp(judge_phone, cur_node->user.phone)) {
		cur_node = cur_node->next;
	}
	if (cur_node == NULL) {
		system("cls");
		printf("*******************************************\n");
		printf("*            未能找到您的信息             *\n");
		printf("*******************************************\n");
		return false;
	}
	else if (!strcmp(judge_secret_code, cur_node->user.secret_code)) {
		system("cls");
		printf("*******************************************\n");
		printf("*            登录成功！                   *\n");
		printf("*******************************************\n");
		/*strcpy(user_node->user.id_nicknames, cur_node->user.id_nicknames);
		strcpy(user_node->user.name, cur_node->user.name);
		strcpy(user_node->user.phone, cur_node->user.phone);
		strcpy(user_node->user.id_name, cur_node->user.id_name);
		strcpy(user_node->user.secret_code, cur_node->user.secret_code);*/
		*cur_user_node = cur_node;

		return true;
		//调用函数


	}
	else {
		system("cls");
		printf("*******************************************\n");
		printf("*            密码错误！                   *\n");
		printf("*******************************************\n");
		return false;
	}


}
//个人信息菜单
int menu_personal_info() {
	system("cls");
	printf("*******************************************\n");
	printf("*           欢迎使用运动场馆系统          *\n");
	printf("*******************************************\n");
	printf("*           请选择您的更改信息            *\n");
	printf("*                1,昵称                   *\n");
	printf("*                2,手机号                 *\n");
	printf("*                3,密码                   *\n");
	printf("*                4,真实姓名               *\n");
	printf("*                5,身份证号码             *\n");
	printf("*                0,退出                   *\n");
	printf("*******************************************\n");
	printf("select->");
	int select = -1;
	scanf_s("%d", &select);
	clear_input_buffer();
	if (select != 1 && select != 2 && select != 0 && select != 3 && select != 4 && select != 5) {

		return select = -1;
	}
	return select;
}
//修改个人信息
void change_info(User_node* user_node, User_list user_list) {
	bool flag_personal_info = true;
	char change_id_nicknames[50];
	char change_phone[50];
	char change_secret_code[50];
	char change_name[50];
	char change_id_name[50];
	while (flag_personal_info) {
		switch (menu_personal_info()) {
		case 1:

			system("cls");
			printf("*           欢迎使用运动场馆系统          *\n");
			printf("*******************************************\n");
			printf("*           更正您的昵称为：              *\n");
			scanf_s("%s", change_id_nicknames,50);
			clear_input_buffer2();
			strcpy_s(user_node->user.id_nicknames,50, change_id_nicknames);

			break;
		case 2:

			system("cls");
			printf("*******************************************\n");
			printf("*           欢迎使用运动场馆系统          *\n");
			printf("*******************************************\n");
			printf("*           更正您的手机号为：            *\n");
			scanf_s("%s", change_phone,50);
			clear_input_buffer2();
			strcpy_s(user_node->user.phone,50, change_phone);
			break;
		case 3:

			system("cls");
			printf("*******************************************\n");
			printf("*           欢迎使用运动场馆系统          *\n");
			printf("*******************************************\n");
			printf("*           更正您的密码为：              *\n");
			scanf_s("%s", change_secret_code,50);
			clear_input_buffer2();
			strcpy_s(user_node->user.secret_code,50,change_secret_code);
			break;
		case 4:

			system("cls");
			printf("*******************************************\n");
			printf("*           欢迎使用运动场馆系统          *\n");
			printf("*******************************************\n");
			printf("*           更正您的真实姓名为：          *\n");
			scanf_s("%s", change_name,50);
			clear_input_buffer2();
			strcpy_s(user_node->user.name,50,change_name);
			break;
		case 5:

			system("cls");
			printf("*******************************************\n");
			printf("*           欢迎使用运动场馆系统          *\n");
			printf("*******************************************\n");
			printf("*           更正您的身份证号码为：        *\n");
			scanf_s("%s", change_id_name,50);
			clear_input_buffer2();
			strcpy_s(user_node->user.id_name,50,change_id_name);
			break;
		case 0:
			flag_personal_info = false;
			system("cls");
			printf("*******************************************\n");
			printf("*                感谢您的使用             *\n");
			printf("*******************************************\n");
			break;
		case -1:
			printf("输入错误，请检查输入选项是否正确！\n");
			break;
		}


	}
	//保存
	printf_user(user_list);//打印部分做测试参考
	save_change_uers_data(&user_list);

}

// 查询功能
// 打印用户信息
void print_user(User_node* user_node) {
	if (user_node != NULL) {
		printf(" 昵称：%s 电话号：%s 密码：%s 名字：%s 身份证号：%s\n",
			user_node->user.id_nicknames,
			user_node->user.phone,
			user_node->user.secret_code,
			user_node->user.name,
			user_node->user.id_name);
	}
}
// 查询用户信息 （昵称 模糊）
void search_user_by_nickname(System sys) {
	char search_nickname[50] = { '\0' };
	int cnt = 0;
	printf(" 输入要查询用户的昵称：");
	scanf_s("%s", search_nickname, 50);
	clear_input_buffer();
	User_node* cur_node = sys.user_link.head;
	while (cur_node != NULL) {
		if (strstr(cur_node->user.id_nicknames, search_nickname) != NULL) {
			cnt++;
			print_user(cur_node);
		}
		cur_node = cur_node->next;
	}
	printf(" 共找到%d条用户信息\n", cnt);
}
// 查询用户信息 （电话）
void search_user_by_phone(System sys) {
	char search_phone[50] = { '\0' };
	int cnt = 0;
	printf(" 输入要查询用户的电话：");
	scanf_s("%s", search_phone, 50);
	clear_input_buffer();
	User_node* cur_node = sys.user_link.head;
	while (cur_node != NULL) {
		if (strcmp(cur_node->user.phone, search_phone)==0) {
			cnt++;
			print_user(cur_node);
		}
		cur_node = cur_node->next;
	}
	printf(" 共找到%d条用户信息\n", cnt);
}
// 查询用户信息 （身份证号）
void search_user_by_id_num(System sys) {
	char search_id_num[50] = { '\0' };
	int cnt = 0;
	printf(" 输入要查询用户的身份证号：");
	scanf_s("%s", search_id_num, 50);
	clear_input_buffer();
	User_node* cur_node = sys.user_link.head;
	while (cur_node != NULL) {
		if (strcmp(cur_node->user.phone, search_id_num) == 0) {
			cnt++;
			print_user(cur_node);
		}
		cur_node = cur_node->next;
	}
	printf(" 共找到%d条用户信息\n", cnt);
}
// 查询用户信息 （真实姓名） 模糊
void search_user_by_name(System sys) {
	char search_name[50] = { '\0' };
	int cnt = 0;
	printf(" 输入要查询用户的姓名：");
	scanf_s("%s", search_name, 50);
	clear_input_buffer();
	User_node* cur_node = sys.user_link.head;
	while (cur_node != NULL) {
		if (strstr(cur_node->user.name, search_name) != NULL) {
			cnt++;
			print_user(cur_node);
		}
		cur_node = cur_node->next;
	}
	printf(" 共找到%d条用户信息\n", cnt);
}

//排序功能
// 查看用户 Order by 姓名
void sort_user_order_by_name(User_list* list) {
	// 咩有和只有一个都不需要排序
	if (!list->head || !list->head->next) return;
	else {
		bool swapped = true;
		User_node* ptr;
		while (swapped) {
			swapped = false;
			ptr = list->head;
			while (ptr->next != NULL) {
				User cur = ptr->user;
				User next = ptr->next->user;
				if (strcmp(cur.name,next.name)>0) {
					ptr->user = next;
					ptr->next->user = cur;
					swapped = true;
				}
				ptr = ptr->next;
			}
		}
	}
}
// 查看用户 Order by phone
void sort_user_order_by_phone(User_list* list) {
	// 咩有和只有一个都不需要排序
	if (!list->head || !list->head->next) return;
	else {
		bool swapped = true;
		User_node* ptr;
		while (swapped) {
			swapped = false;
			ptr = list->head;
			while (ptr->next != NULL) {
				User cur = ptr->user;
				User next = ptr->next->user;
				if (strcmp(cur.phone, next.phone) > 0) {
					ptr->user = next;
					ptr->next->user = cur;
					swapped = true;
				}
				ptr = ptr->next;
			}
		}
	}
}

